﻿using System;

namespace Alfa.Units {
	public struct Fahrenheit : ITemperatureUnit, IComparable<Fahrenheit>, IEquatable<Fahrenheit> {
		private decimal degrees;
		public Fahrenheit(decimal Degrees) {
			this.degrees = Degrees;
		}

		public static explicit operator Celsius(Fahrenheit fahrenheit) {
			return new Celsius((fahrenheit.degrees - 32m) * 5m / 9m);
		}

		public static explicit operator Kelvin(Fahrenheit fahrenheit) {
			return new Kelvin((fahrenheit.degrees + 459.67m) * 5m / 9m);
		}

		public static explicit operator Rankine(Fahrenheit fahrenheit) {
			return new Rankine(fahrenheit.degrees + 459.67m);
		}

		public static implicit operator decimal(Fahrenheit fahrenheit) {
			return fahrenheit.degrees;
		}

		decimal ITemperatureUnit.Degrees { get { return this.degrees; } }

		string ITemperatureUnit.Symbol { get { return "°F"; } }

		public int CompareTo(object obj) {
			if (obj is ITemperatureUnit) {
				return ((ITemperatureUnit)this).CompareTo((ITemperatureUnit)obj);
			} else
				throw new InvalidCastException(obj.GetType( ).Name + " does not implement ITemperatureUnit");
		}

		int IComparable<ITemperatureUnit>.CompareTo(ITemperatureUnit other) {
			return this.degrees.CompareTo(((Fahrenheit)other).degrees);
		}

		bool IEquatable<ITemperatureUnit>.Equals(ITemperatureUnit other) {
			return this.degrees.Equals(((Fahrenheit)other).degrees);
		}

		int IComparable<Fahrenheit>.CompareTo(Fahrenheit other) {
			return this.degrees.CompareTo(other.degrees);
		}

		bool IEquatable<Fahrenheit>.Equals(Fahrenheit other) {
			return this.degrees.Equals(other.degrees);
		}
	}
}
